package com.yanyeori.framework.jira.jiraclient;

import net.sf.json.JSON;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Represents issue watches.
 */
public class Watches extends Resource {

    private String name = null;
    private int watchCount = 0;
    private boolean isWatching = false;
    private List<User> watchers = new ArrayList<>();

    /**
     * Creates watches from a JSON payload.
     *
     * @param restclient REST client instance
     * @param json       JSON payload
     */
    protected Watches(RestClient restclient, JSONObject json) {
        super(restclient);

        if (json != null)
            deserialise(json);
    }

    private void deserialise(JSONObject json) {
        self = Field.getString(json.get("self"));
        id = Field.getString(json.get("id"));
        watchCount = Field.getInteger(json.get("watchCount"));
        isWatching = Field.getBoolean(json.get("isWatching"));
        watchers = Field.getResourceArray(User.class, json.get("watchers"), null);
    }

    /**
     * Retrieves the given watches record.
     *
     * @param restclient REST client instance
     * @param issue      Internal JIRA ID of the issue
     * @return a watches instance
     * @throws JiraException when the retrieval fails
     */
    public static Watches get(RestClient restclient, String issue) throws JiraException {
        JSON result;

        try {
            result = restclient.get(getBaseUri() + "issue/" + issue + "/watchers");
        } catch (Exception ex) {
            throw new JiraException("Failed to retrieve watches for issue " + issue, ex);
        }

        if (!(result instanceof JSONObject))
            throw new JiraException("JSON payload is malformed");

        return new Watches(restclient, (JSONObject) result);
    }

    @Override
    public String toString() {
        return Integer.toString(getWatchCount());
    }

    public int getWatchCount() {
        return watchCount;
    }

    public boolean isWatching() {
        return isWatching;
    }

    public List<User> getWatchers() {
        return watchers;
    }
}
